﻿#ifndef df_clock_h__2013_12_24__22_00
#define df_clock_h__2013_12_24__22_00


#define DF_TIME_CLOCK(_num,_fun) DF_TIME_CLOCK_MSG("",_num,_fun)

#ifdef WIN32
#	define DF_LONGFORMAT "%I64d"
#else
#	define DF_LONGFORMAT "%lld"
#endif

#define DF_PRINT printf


//获取表达式fun循环num次的耗时(精确至0.1毫秒)
#define DF_TIME_CLOCK_MSG(_msg,_num,_fun) {long long DF_MIX_LINENAME(tick,__LINE__)=df::GetTickClock();\
for (long long DF_MIX_LINENAME(I_I, __LINE__) = 0; DF_MIX_LINENAME(I_I, __LINE__) < _num; DF_MIX_LINENAME(I_I, __LINE__)++)	\
	_fun											\
	long long DF_MIX_LINENAME(tick2, __LINE__) = df::GetTickClock(); 		\
	DF_PRINT(_msg " time : " DF_LONGFORMAT "\r\n", (DF_MIX_LINENAME(tick2, __LINE__) - DF_MIX_LINENAME(tick, __LINE__))); }

namespace df
{
#ifdef WIN32
	//获取每秒多少CPU Performance Tick
	inline static long long GetCpuFrequency()
	{
		static long long fre = 0;
		if (fre == 0)
		{
			LARGE_INTEGER frequency = { { 0 } };
			QueryPerformanceFrequency(&frequency);
			fre = frequency.QuadPart;
		}
		return fre;

	}

	//用于得到高精度计时器的值
	inline static long long GetCpuCounter()
	{
		LARGE_INTEGER counter = { { 0 } };
		QueryPerformanceCounter(&counter);
		return counter.QuadPart;
	}

	//*******************************************
	// Summary : 获取启动时间精确至0.1毫秒
	//*******************************************
	inline static long long GetTickClock()
	{
		return GetCpuCounter() * 10000 / GetCpuFrequency();
	}

#else
#include <sys/time.h>
	static inline long long GetTickClock()
	{
		timeval tv;
		gettimeofday(&tv, 0);
		return tv.tv_sec * 10000 + tv.tv_usec / 100;
	}
#endif

	inline static long GetTickMilli()
	{
		return std::clock() * 1000 / CLOCKS_PER_SEC;
	}


	template<class _FuncT>
	static void TickClock(_FuncT Func, long long num = 1)
	{
		long long tick = GetTickClock();
		for (long long i = 0; i < num; i++)
		{
			Func();
		}
		long long resTime = GetTickClock() - tick;

		printf("耗时:" DF_LONGFORMAT "\r\n", resTime);

	}

}


#endif // df_clock_h__2013_12_24__22_00
